// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.assets.controller.request.QueryAssetsSupplierReq;
import com.liuxinlong.modules.assets.controller.request.UpdateAssetsSupplierReq;
import com.liuxinlong.modules.assets.service.AssetsSupplierService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.AssetsSupplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 固资供应商管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-01-02
 */
@RestController
@RequestMapping("/assets/supplier")
public class AssetsSupplierController {
    @Autowired
    private AssetsSupplierService assetsSupplierService;

    /**
     * 分页查询供应商管理信息列表
     *
     * @param req 请求信息
     * @return 供应商管理信息列表
     */
    @PostMapping("page")
    public BaseResp pageAssetsSupplierList(@RequestBody QueryAssetsSupplierReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("supplierCode", req.getSupplierCode());
        queryMap.put("supplierName", req.getSupplierName());
        queryMap.put("domain", req.getDomain());
        List<Map<String, Object>> dataList = assetsSupplierService.pageAssetsSupplierInfo(queryMap);
        int totalNum = assetsSupplierService.getAssetsSupplierInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询供应商信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增供应商管信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增供应商管信息", business = LogBusinessConstants.SUPPLIER_MANAGEMENT, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addAssetsSupplierInfo(@RequestBody UpdateAssetsSupplierReq req) {
        AssetsSupplier assetsSupplier = new AssetsSupplier();
        assetsSupplier.setSupplierCode(req.getSupplierCode());
        assetsSupplier.setSupplierName(req.getSupplierName());
        assetsSupplier.setDomain(req.getDomain());
        assetsSupplier.setRemark(req.getRemark());
        assetsSupplierService.addAssetsSupplierInfo(assetsSupplier);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增供应商管信息成功");
        return resp;
    }

    /**
     * 修改供应商信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改供应商信息", business = LogBusinessConstants.SUPPLIER_MANAGEMENT, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateAssetsSupplierInfo(@RequestBody UpdateAssetsSupplierReq req) {
        AssetsSupplier assetsSupplier = new AssetsSupplier();
        assetsSupplier.setId(req.getId());
        assetsSupplier.setSupplierCode(req.getSupplierCode());
        assetsSupplier.setSupplierName(req.getSupplierName());
        assetsSupplier.setDomain(req.getDomain());
        assetsSupplier.setRemark(req.getRemark());
        assetsSupplierService.updateAssetsSupplierInfo(assetsSupplier);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改供应商息成功");
        return resp;
    }

    /**
     * 删除供应商信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除供应商信息", business = LogBusinessConstants.SUPPLIER_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteAssetsSupplierInfo(@RequestBody BaseDeleteReq req) {
        assetsSupplierService.deleteAssetsSupplierInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除供应商信息成功");
        return resp;
    }

    /**
     * 同步供应商信息
     *
     * @return 成功标识
     */
    @GetMapping("sync")
    @LogAnnotation(value = "同步供应商信息", business = LogBusinessConstants.SUPPLIER_MANAGEMENT, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp syncSupplierInfo() {
        assetsSupplierService.syncSupplierInfo();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("同步供应商信息成功");
        return resp;
    }
}
